package com.xyz.frame.api.controller.shzhyun;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.shzhyun.pay.Service.WechatPayManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
@Slf4j
public class WechatPayController {
    @Autowired
    private WechatPayManager wechatPayManager;

//    @RequestMapping(value = "/pay/createOrderNative",method = RequestMethod.GET)
//    public String createOrderNative(){
//        log.info("createOrderNative jsonObject:{}");
//        return wechatPayManager.createOrderNative();
//    }

    /**
     * curl -H "Content-Type:application/json" -X POST -d '{"amount":"12","openid":"ofzge5fkD-PrlO03GgErvJY3p614"}' http://127.0.0.1:8888/chartapi/pay/createOrderJsApi
     * @param dto
     * @return
     */
    @RequestMapping(value = "/pay/createOrderJsApi",method = RequestMethod.POST)
    public PrepayWithRequestPaymentResponse createOrderJsApi(@RequestBody JSONObject dto){
        log.info("createOrderJsApi jsonObject:{}",dto);
        return wechatPayManager.createOrderJsApi(dto);
    }

//    @RequestMapping(value = "/pay/notifyNative",method = RequestMethod.POST)
//    public String  notifyNative(HttpServletRequest servletRequest, HttpServletResponse response) {
//        log.info("request:{}",JSONObject.toJSONString(servletRequest));
//        return wechatPayManager.callbackNative(servletRequest,response);
//    }


    @RequestMapping(value = "/pay/notifyJsApi",method = RequestMethod.POST)
    @Async
    public String  notifyJsApi(HttpServletRequest servletRequest, HttpServletResponse response) {
        log.info("notifyJsApi request:{}",JSONObject.toJSONString(servletRequest));
        String res = "success";
        res = wechatPayManager.callbackJsApi(servletRequest, response);
        return res;
    }

}
